<!-- 表格组件 -->
<script lang="tsx" setup>
import { toRaw } from "vue";

import type { TableColumnOption } from "@/components/table-widget/table-widget.vue";
import TableWidget from "@/components/table-widget/table-widget.vue";
import { useUserStore } from "@/stores/user";
import { useCompanyStore } from "@/stores/company";
import {
	INTEGRITY_LEVEL_OPTIONS,
	SUPERVISE_TYPE_OPTIONS,
} from "@/config/enum-map";
import {
	getEnumOptionsField,
	ENTERPRISE_ECONOMIC_TYPE_OPTIONS,
} from "@/config/enum-options";
import { whManagerBsTbEnterpriseDeleteByIdIdUsingPost } from "@/genapi/wh";

import { injectState } from "../state";

defineOptions({ name: "Sheet" });

const {
	selectedList,
	tableRef,
	detailDialogRef,
	page: { records, isLoading, execute },
} = injectState()!;

const userStore = useUserStore();

const companyStore = useCompanyStore();

const router = useRouter();

watch(records, () => tableRef.value?.table?.clearSelection());

const onDelete = async (row: (typeof records.value)[0]) => {
	await ElMessageBox.confirm(
		`您即将删除【${row.name ?? "未知"}】，删除后不可恢复，是否确认删除？`,
		{
			type: "error",
			title: "提示",
		},
	);
	await whManagerBsTbEnterpriseDeleteByIdIdUsingPost({ path: { id: row.id! } });
	ElMessage.success("删除成功");
	await execute();
};

const jumpToCompany = (row) => {
	userStore.userInfo.systemUser.relatedId = row.id;
	companyStore.refresh();
	router.replace({ path: "/company/information" });
};

const columns: TableColumnOption<(typeof records.value)[0]>[] = [
	{
		type: "selection",
		fixed: "left",
		noExport: true,
	},
	{
		label: "序号",
		type: "index",
		fixed: "left",
		width: 80,
	},
	{
		label: "企业名称",
		formatter: (row) => (
			<>
				<span
					v-show={row.inBlacklist === "1"? true:false}
					class="mr-8px rd-2px bg-#363636 px-4px py-2px text-12px text-#fff!"
				>
					黑
				</span>
				{row.name ?? "-"}
			</>
		),
	},
	{
		label: "企业负责人",
		formatter: (row) => row.mainMasterName ?? "-",
	},
	{
		label: "负责人联系电话",
		formatter: (row) => row.mainMasterPhone ?? "-",
	},
	{
		label: "经济类型",
		formatter: (row) =>
			getEnumOptionsField(
				ENTERPRISE_ECONOMIC_TYPE_OPTIONS,
				"label",
				row.economicType,
				"-",
			) ?? "-",
	},
	{
		label: "安全生产许可编号",
		formatter: (row) => row.safeProductAuthno ?? "-",
	},
	{
		label: "有效期限",
		formatter: (row) => `${row.expireDateStart}--${row.expireDateEnd}`,
	},
	{
		label: "企业员工",
		formatter: (row) => row.employeeNum ?? "-",
	},
	{
		label: "诚信等级",
		formatter: (row) =>
			getEnumOptionsField(
				INTEGRITY_LEVEL_OPTIONS,
				"label",
				row.integrityLevel,
				"-",
			) ?? "-",
	},
	{
		label: "监管分类",
		formatter: (row) =>
			getEnumOptionsField(
				SUPERVISE_TYPE_OPTIONS,
				"label",
				row.superviseType,
				"-",
			) ?? "-",
	},
	{
		label: "操作",
		export: false,
		fixed: "right",
		width: "200",
		noExport: true,
		formatter(row) {
			return (
				<>
					<el-button
						size="small"
						type="primary"
						text
						onClick={() => jumpToCompany(row)}
					>
						详情
					</el-button>
					<el-button
						size="small"
						type="primary"
						text
						onClick={() => detailDialogRef.value?.trigger("write", row)}
					>
						编辑
					</el-button>
					<el-button
						size="small"
						type="primary"
						text
						onClick={() => onDelete(row)}
					>
						删除
					</el-button>
				</>
			);
		},
	},
];
</script>

<template>
	<TableWidget
		ref="tableRef"
		v-loading="isLoading"
		:data="records"
		:columns="columns"
		h="100%!"
		@selection-change="(rows:any) => (selectedList = toRaw(rows))"
	/>
</template>
